package com.bifrost.admin.controller;

import com.bifrost.annotation.CurrentAdmin;
import com.bifrost.common.base.ServerResponse;
import com.bifrost.entity.User;
import com.bifrost.exception.ParamException;
import com.bifrost.model.param.RoleParam;
import com.bifrost.service.AdminUserService;
import com.bifrost.service.RoleService;
import com.bifrost.util.BeanValidator;
import com.bifrost.validator.group.RoleAdd;
import com.bifrost.validator.group.RoleUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/role")
@Api("角色相关接口")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    private AdminUserService userService;

    /**
     * 添加角色，并同时给角色分配权限
     * @param permissionIds 给角色分配的权限（可以为空，表示暂时不给改角色分配权限）
     * @param roleParam 角色
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("增加角色")
    public ServerResponse add(Integer[] permissionIds, RoleParam roleParam) throws ParamException {
        Map<String,String> errors = BeanValidator.validate(roleParam, RoleAdd.class);
        if(!errors.isEmpty())
            return ServerResponse.createByError(errors.values());
        return roleService.add(permissionIds,roleParam);
    }

    /**
     * 获取角色列表
     * @param currentPage 当前需要显示的页面的页码
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation("获取角色列表")
    public ServerResponse list(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize,String roleName) {
        return roleService.list(currentPage,pageSize,roleName);
    }

    /**
     * 获取角色列表(添加用户时获取角色列表的时候使用这个接口）
     * @return
     */
    @RequestMapping(value = "/list/add",method = RequestMethod.GET)
    @ApiOperation("获取角色列表")
    public ServerResponse listForUserAdd() {
        return roleService.listForUserAdd();
    }

    /**
     * 更新角色
     * @param roleParam 需要更新的角色（该对象的id属性一定不能为空）
     * @param permissionIds 该角色对应的权限（可以为空）
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation("更新角色")
    public ServerResponse edit(RoleParam roleParam, Integer[] permissionIds, @CurrentAdmin Long currentUser) {
        Map<String,String> errors = BeanValidator.validate(roleParam, RoleUpdate.class);
        if(!errors.isEmpty())
            return ServerResponse.createByError(errors.values());
        return roleService.update(roleParam,permissionIds,userService.findById(currentUser) );
    }

    /**
     * 获取角色详情
     * @param roleId 角色对应的id
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    @ApiOperation("获取角色详情")
    public ServerResponse detail(Integer roleId ) {
        return roleService.getDetail(roleId);
    }

    /**
     *批量删除角色
     * @param roleIds
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ApiOperation("批量删角色")
    public ServerResponse batchDelete(Integer[] roleIds) {
        return roleService.batchDelete(roleIds);
    }
}
